package com.example.cyk.myapplication.utils;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.widget.Toast;

/**
 * Created by cyk on 2014-10-06.
 *
 * 일반 팝업 ex
 * onPopup("제목", "내용");
 *
 * 종료 팝업 ex
 * onExitPopup("종료하시겠습니까?");
 *
 * 리스트 팝업 ex
 * final CharSequence[] items = {"Red", "Green", "Blue"};
 * onListPopup("Pick a color", items);
 *
 * 체크 팝업 ex
 * final CharSequence[] items = {"Red", "Green", "Blue"};
 * final boolean[] itemsChecked = {false, true, false};
 * onCheckPopup("Pick a color", items, itemsChecked);
 */

public class PopupUtils {

    public PopupUtils(Context $context){
        m_context = $context;
    }
    private Context m_context;

    public void onToast(String $msg){
        Toast.makeText(m_context, $msg, Toast.LENGTH_SHORT).show();
    }
    public void onPopup(String $menu, String $msg) {
        AlertDialog.Builder ab = null;
        ab = new AlertDialog.Builder( m_context );
        ab.setMessage($msg);
        ab.setPositiveButton(android.R.string.ok, null);
        ab.setTitle($menu);
        ab.show();
    }
    public void onExitPopup(String $msg){
        AlertDialog.Builder builder = new AlertDialog.Builder(m_context);
        builder.setMessage($msg)
                .setCancelable(false)
                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        //onToast("Yes " + Integer.toString(id));
                        ExitUtils exitutil = new ExitUtils();
                        exitutil.onFinish();
                    }
                })
                .setNegativeButton("No", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        onToast("No " + Integer.toString(id));
                        dialog.cancel();
                    }
                });
        AlertDialog alert = builder.create();
        alert.show();
    }

    public void onListPopup(String $msg, CharSequence[] $items){
        AlertDialog.Builder builder = new AlertDialog.Builder(m_context);
        builder.setTitle($msg);
        builder.setItems($items, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int item) {
                onToast(Integer.toString(item));
            }
        });
        AlertDialog alert = builder.create();
        alert.show();
    }

    public void onCheckPopup(String $msg, CharSequence[] $items, boolean[] $itemsChecked){
        AlertDialog.Builder builder = new AlertDialog.Builder(m_context);
        builder.setTitle($msg);
        builder.setMultiChoiceItems( $items, $itemsChecked, new DialogInterface.OnMultiChoiceClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                onToast(Integer.toString(which) + "  한글-" + Boolean.toString(isChecked));
            }
        });
        AlertDialog alert = builder.create();
        alert.show();
    }
}